package com.feihong.txgw.utils.shorCut;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.feihong.txgw.R;
import com.feihong.txgw.model.shorcut.ShorCutEntity;
import com.feihong.txgw.ui.activity.kaika.ScanBarCodeActivity;
import com.feihong.txgw.ui.activity.order.MyOrderActivity;
import com.feihong.txgw.ui.activity.user.LiuLiangChaXunActivity;
import com.feihong.txgw.ui.activity.user.MyShopCartActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ShorCut
 * @package com.feihong.txgw.utils.shorCut
 * @data 2018/6/21 19:08
 * @Describe
 * @Change
 */
public class ShorCut {
    private ShortcutManager mShortcutManager;
    private Context mContext;

    public ShorCut(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            mContext = context;
            mShortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
        }
    }
    /**
     * 设置动态快捷方式
     */

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    private void initShortCuts(List<ShorCutEntity> data){
        if(mShortcutManager == null){
            return;
        }
        List<ShortcutInfo> dynamicShortcuts = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            ShorCutEntity shorCutEntity = data.get(i);
            Intent intent = null;
            try {
                intent = new Intent(mContext, Class.forName(shorCutEntity.getActivityName()));
                intent.setAction(Intent.ACTION_VIEW);
                ShortcutInfo info = new ShortcutInfo.Builder(mContext,shorCutEntity.getId())
                        .setShortLabel(shorCutEntity.getShortTitle())
//                        .setLongLabel(shorCutEntity.getLongTilte())
                        .setIcon(Icon.createWithResource(mContext, shorCutEntity.getIcon()))
                        .setIntent(intent)
                        .build();
                dynamicShortcuts.add(info);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }

        mShortcutManager.setDynamicShortcuts(dynamicShortcuts);
    }
    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    public void initShortCus() {

        List<ShorCutEntity> shortCutEntities = new ArrayList<>();
        ShorCutEntity mShortCutEntity = new ShorCutEntity();
        mShortCutEntity.setId("shopCar");
        mShortCutEntity.setShortTitle("购物车");
        mShortCutEntity.setIcon(R.mipmap.home_tab3_selected);
        mShortCutEntity.setActivityName(MyShopCartActivity.class.getName());
        shortCutEntities.add(mShortCutEntity);

        mShortCutEntity = new ShorCutEntity();
        mShortCutEntity.setActivityName(ScanBarCodeActivity.class.getName());
        mShortCutEntity.setShortTitle("扫卡开户");
        mShortCutEntity.setIcon(R.mipmap.icon_saoma);
        mShortCutEntity.setId("ScanBar");
        shortCutEntities.add(mShortCutEntity);

        mShortCutEntity = new ShorCutEntity();
        mShortCutEntity.setId("order");
        mShortCutEntity.setShortTitle("订单");
        mShortCutEntity.setIcon(R.mipmap.shorcut_order);
        mShortCutEntity.setActivityName(MyOrderActivity.class.getName());
        shortCutEntities.add(mShortCutEntity);

        mShortCutEntity = new ShorCutEntity();
        mShortCutEntity.setActivityName(LiuLiangChaXunActivity.class.getName());
        mShortCutEntity.setShortTitle("流量查询");
        mShortCutEntity.setIcon(R.mipmap.query_ll_icon);
        mShortCutEntity.setId("llcx");
        shortCutEntities.add(mShortCutEntity);

        initShortCuts(shortCutEntities);
    }

    /**
     * 更新快捷方式标题
     * 根据id来更新
     *
     * @param id
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void updataDynamicStatus(String id) {
        if(mShortcutManager == null){
            return;
        }
//        Intent intent = new Intent(mContext, SecondActivity.class);
//        intent.setAction(Intent.ACTION_VIEW);
        ShortcutInfo info = new ShortcutInfo.Builder(mContext, id)
                .setShortLabel("已修改")
                .setLongLabel("功能:已修改修改")
                .setIcon(Icon.createWithResource(mContext, R.mipmap.icon_qq))
//                .setIntent(intent)
                .build();
        mShortcutManager.updateShortcuts(Arrays.asList(info));
    }

    /**
     * 禁用快捷方式
     *
     * @param id
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void disableShortCus(String id) {
        if(mShortcutManager == null){
            return;
        }
        List<ShortcutInfo> mList = mShortcutManager.getDynamicShortcuts();
        for (ShortcutInfo shortcutInfo : mList) {
            if (shortcutInfo.getId().equals(id)) {
                // 暂时效果为界面上不显示被禁用掉的快捷方式
                mShortcutManager.disableShortcuts(Arrays.asList(shortcutInfo.getId()), "此功能已删除");
            }
        }

    }

    /**
     * 删除指定快捷方式
     * @param id
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void removeShortCus(String id) {
        if(mShortcutManager == null){
            return;
        }
        List<ShortcutInfo> shortcutInfos = mShortcutManager.getDynamicShortcuts();
        for (ShortcutInfo info : shortcutInfos) {
            if (info.getId().equals(id)) {
                mShortcutManager.removeDynamicShortcuts(Arrays.asList(info.getId()));
            }
        }
    }

    /**
     * 删除所有快捷方式
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void removeAllShortCus(){
        mShortcutManager.removeAllDynamicShortcuts();
    }
}
